package com.huawei.hwid.core.b.a.a;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.hms.support.hianalytics.HiAnalyticsConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.d.p;
import com.huawei.hwid.core.datatype.UserInfo;
import com.huawei.hwid.core.helper.handler.ErrorStatus;
import com.huawei.hwid.f.g;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class h extends com.huawei.hwid.core.b.a.a {
    private String h = HwAccountConstants.TYPE_TENCENT;
    private String i;
    private UserInfo j;
    private String k;
    private String l;
    private String m = "/IUserInfoMng/updateUserInfo";

    static class a extends com.huawei.hwid.core.helper.handler.a {
        private CloudRequestHandler a;

        public a(Context context, CloudRequestHandler cloudRequestHandler) {
            super(context);
            this.a = cloudRequestHandler;
        }

        public void a(Bundle bundle) {
            super.a(bundle);
            this.a.onFinish(bundle);
        }

        public void b(Bundle bundle) {
            super.b(bundle);
            ErrorStatus errorStatus = (ErrorStatus) bundle.getParcelable("requestError");
            if (errorStatus != null) {
                this.a.onError(errorStatus);
            } else {
                this.a.onError(new ErrorStatus(32, "ErrorStatus is null"));
            }
        }
    }

    public h(String str, String str2, String str3, UserInfo userInfo, String str4, Bundle bundle) {
        g(str);
        if (!TextUtils.isEmpty(str4)) {
            h(str4);
        }
        a(userInfo);
        b(70005002);
        b(70006007);
        b(70005003);
        b(70005005);
        b(70001201);
        b(70007005);
    }

    private void f(String str) {
        this.l = str;
    }

    public String j() throws IllegalArgumentException, IllegalStateException, IOException {
        OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            XmlSerializer b = p.b(byteArrayOutputStream);
            b.startDocument("UTF-8", Boolean.valueOf(true));
            b.startTag(null, "UpdateUserInfoReq");
            p.a(b, "version", "27120");
            p.a(b, "userID", this.i);
            p.a(b, CloudAccountManager.KEY_REQCLIENTTYPE, this.h);
            p.a(b, "password", this.k);
            p.a(b, UserInfo.LANGUAGECODE, this.l);
            if (this.j != null) {
                b.startTag(null, "userInfo");
                if (!(this.l == null || this.l.isEmpty())) {
                    this.j.setLanguageCode(this.l);
                }
                UserInfo.setUserInfoIntag(b, this.j);
                b.endTag(null, "userInfo");
            }
            b.endTag(null, "UpdateUserInfoReq");
            b.endDocument();
            String byteArrayOutputStream2 = byteArrayOutputStream.toString("UTF-8");
            return byteArrayOutputStream2;
        } finally {
            try {
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.d("UpdateUserInfoRequest", "IOException", true);
            }
        }
    }

    public void a(String str) throws XmlPullParserException, IOException {
        XmlPullParser a = p.a(str.getBytes("UTF-8"));
        for (int eventType = a.getEventType(); 1 != eventType; eventType = a.next()) {
            String name = a.getName();
            switch (eventType) {
                case 2:
                    if (HiAnalyticsConstant.BI_KEY_RESUST.equals(name)) {
                        this.b = g.a(a.getAttributeValue(null, "resultCode"));
                    }
                    e.e("UpdateUserInfoRequest", "result:", true);
                    if (this.b != 0) {
                        if (!"errorCode".equals(name)) {
                            if (!"errorDesc".equals(name)) {
                                break;
                            }
                            this.d = a.nextText();
                            break;
                        }
                        this.c = g.a(a.nextText());
                        break;
                    }
                    break;
                default:
                    break;
            }
        }
    }

    public String b() {
        return this.m;
    }

    private void g(String str) {
        this.i = str;
    }

    private void a(UserInfo userInfo) {
        this.j = userInfo;
    }

    private void h(String str) {
        this.k = str;
    }

    public Bundle c() {
        return super.c();
    }

    public String c(Context context) {
        return b.f(context);
    }

    public void a(Context context, com.huawei.hwid.core.b.a.a aVar, String str, CloudRequestHandler cloudRequestHandler) {
        f(b.f(context));
        com.huawei.hwid.core.b.a.e.a(context, aVar, str, a(context, aVar, new a(context, cloudRequestHandler)));
    }
}
